// pages/friends/addOne/addOne.js
var app=getApp()
var url=app.globalData.url
var qytel=''
var yzm=''
var mainId=''
var label=''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    names:"添加亲友",
    flag:true,
    Length: 4,    //输入框个数 
    isFocus: true,  //聚焦 
    Value: "", 
    label:["网友","家人","朋友"] ,
    number:60
  },
  back(){
    wx.navigateBack(1)
  },
  getCount(e){
   const that=this
   var tel=e.detail.value
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
   if(tel.length==11){
     if(!reg.test(tel)){
       wx.showToast({
         title: '手机格式错误',
         icon:"loading",
         duration:1000
       })
     }else{
       wx.showLoading({
         title: '正在查询',
       })
       that.checkTel(tel)
     }
   
   }
  },
  addlabel(e){
    label = e.currentTarget.dataset.label
   this.setData({
     Label:e.currentTarget.dataset.label,
     labIndex:e.currentTarget.id
   })
  },
  checkTel(e){
   qytel=e
    wx.request({
      url: url + '/relatives_friends/tf',
      data:{
        phone:e
      },
      header:{
        "content-type":"application/json"
      },
      success(res){
        wx.hideLoading();

        if(res.data.code==0){
          wx.showToast({
            title: '验证通过',
            icon:"success",
            duration:1000
          })
        }else{
          wx.showToast({
            title: '该号码未注册',
            icon:"loading",
            duration:1000
          })
        }
      }
    })
  },
  // 返回绑定的唯一主键
  getreback(){
    var uer = wx.getStorageSync('user')
    wx.request({
      url: url+'/relatives_friends/insert',
      data:{
        number: uer.wxUsersAccount,
        phone:qytel
      },
      header:{
        "content-type":"application/json"
      },
      success(res){
        if(res.data.code==1){
          mainId=res.data.data
        }
      }
    })
  },
  Focus(e) {
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value;
 
    that.setData({
      Value: inputValue,
    })
    if (inputValue.length == 4) {
        if(inputValue==yzm){
          that.setData({
            currentTab: 2
          })
          that.getreback()
        }else{
          wx.showToast({
            title: '验证码错误',
            icon:"loading",
            duration:1000
          })
        }
     
    }
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  }, 
  nextbtn(){
    if(qytel.length<=0){
      wx.showToast({
        title: '请填写手机号',
        icon:"loading",
        duration:1000
      })
    }else{
      const that = this
      that.setData({
        currentTab: 1
      })
      var count = 60;
      var interver = setInterval(function () {
        count = count - 1
        that.setData({
          number: count
        })
        if (count == 0) {
          count = 60
          that.setData({
            number: count
          })
          clearInterval(interver)
        }
      }, 1000)
      that.sendyzm()
    }

  },
  sendyzm(){
    var uer=wx.getStorageSync('user')
    wx.request({
      url: url+'/ten_xun/qy',
      data:{
        phone1: uer.wxUsersPhone,
        phone2:qytel
      },
      header:{
        "content-type":"application/json"
      },
      success(res){
        console.log(res.data)
         if(res.data.code==1){
                yzm=res.data.data
         }
      }
    })
  },
  addfinish(){
    if(label.length<=0){
      wx.showToast({
        title: '请添加标签',
        icon:"loading",
        duration:1500
      })
    }else{
    wx.request({
      url: url+'/relatives_friends/update',
      data:{
        id:mainId,
        label:label
      },
      header:{
        "content-type": "application/json"
      },success(res){
        if(res.data.code==0){
          wx.showToast({
            title: '添加成功',
            icon:"success",
            duration:1000
          })
          setTimeout(function(){
            wx.navigateBack(1)
          },1000)
        }
      }
    })
    }
  },
  getlabel(e){
    var lebel=e.detail.value
    label=lebel
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})